<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");

$errMsg = "";
if(isset($_REQUEST["reqtype"])){	
	if($_REQUEST["reqtype"] == "save"){
		$errMsg .= puAttrMod();
	}
	$process_unit = sxREQUEST("process_unit");
	$alias = sxREQUEST("alias");
	$attribute = sxREQUEST("attribute");
	$description = sxREQUEST("description");
	$read_only = (isset($_REQUEST["read_only"]))?"Y":"N";
	$note = sxREQUEST("note");
}else{
	$process_unit = sxREQ("pu");
	$alias = sxREQ("alias");
	$attribute = sxREQ("attr");
	$description =  "";
	$read_only = "";
	$note =  "";
}
if($process_unit != ""){
	if($alias == ""){
		$errMsg .= _t("L'alias non &egrave; definito<br>");
	}
	if($attribute == ""){
		$errMsg .= _t("L'attribute non &egrave; definito<br>");
	}
	if($errMsg == ""){
		$alias = addslashes(sxGPC(trim($_REQUEST["alias"])));
		$entity = addslashes(sxGPC(trim($_REQUEST["ent"])));
		$attribute = addslashes(sxGPC(trim($_REQUEST["attr"])));
		$sSQL = "SELECT ali.process_unit, ali.alias, ali.entity, a.attribute, pat.description,  pat.note, " .
				"    IFNULL(pat.read_only,ali.read_only) AS read_only, pat.attribute AS pat_attr " .
				" FROM pu_alias ali " .
				" INNER JOIN attribute a ON a.entity = ali.entity " .
				" LEFT JOIN pu_attribute pat ON pat.process_unit = ali.process_unit AND pat.alias = ali.alias " .
				"    AND pat.entity = ali.entity AND pat.attribute = a.attribute " .
				" WHERE ali.process_unit='$process_unit' AND ali.alias='$alias' " .
				" AND a.attribute = '$attribute'";
		$objSDAO->query($sSQL,"sel");
		if($row = $objSDAO->fetch("sel")){
			$description = $row["description"];
			$entity = $row["entity"];
			$read_only = $row["read_only"];
			$note = $row["note"];
		}
	}	
}else{
	$errMsg .= _t("Il processo non &egrave; definito<br>");
}
//output HTML
$aOpz = array("tit2"=>_t("Modifica attribute"), "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/ajax.js"></script>  
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
	var aElemObbl = Array("attribute", "entity");
	var aDescrElemObbl = Array("attribute", "entity");
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"puAttrMod.php\" method=\"POST\" enctype=\"multipart/form-data\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"xsn\" id=\"xsn\" value=\"".XSN."\">");
echo("<input type=\"hidden\" name=\"process_unit\" id=\"process_unit\" value=\"$process_unit\">");
echo("<input type=\"hidden\" name=\"entity\" id=\"entity\" value=\"$entity\">");
echo("<input type=\"hidden\" name=\"attribute\" id=\"attribute\" value=\"$attribute\">");
echo("<input type=\"hidden\" name=\"alias\" id=\"alias\" value=\"$alias\">");
//output comandi
$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("Salva i dati dell'attribute"),
										"text"=>_t("conferma")
										),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>"cancel"
										)
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>"._t("Attributo")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("processo")." (*)</td>" .
			"<td class=\"text01\">$process_unit</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("alias")." (*)</td>" .
			"<td class=\"text01\">$alias</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("entity")." (*)</td>" .
			"<td class=\"text01\">$entity</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("attribute")." (*)</td>" .
			"<td class=\"text01\">$attribute</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("description")."</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"description\" id=\"description\" maxlength=\"80\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
$chk = "";
if($read_only == "Y")
	$chk = "CHECKED";
echo("<tr>" .
			"<td class=\"labl01\">"._t("sola lettura")."</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"checkbox\" name=\"read_only\" id=\"read_only\" $chk></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("note")."</td>" .
			"<td class=\"text01\"><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">".sxHE($note)."</textarea></td>" .
		"</tr>\n");
echo("</table>");

echo(commandsHTML($aCmd));

echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function puAttrMod($redirOnEnd = true){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	
	$process_unit = addslashes(sxGPC(trim($_REQUEST["process_unit"])));
	$alias = addslashes(sxGPC(trim($_REQUEST["alias"])));
	$entity = addslashes(sxGPC(trim($_REQUEST["entity"])));
	$attribute = addslashes(sxGPC(trim($_REQUEST["attribute"])));
	if($errMsg != ""){ return $errMsg; }
	
	$read_only = isset($_REQUEST["read_only"])?"Y":"N";
	//controllo che l'attribiuto non esista gi@
	$sSQL = "SELECT count(*) as num FROM pu_attribute " .
			"WHERE process_unit='".$_REQUEST["process_unit"]."' " .
			"AND entity='".$_REQUEST["entity"]."'" . 
			"AND attribute='".$_REQUEST["attribute"]."'" . 
			"AND alias='".$_REQUEST["alias"]."'";
	$num = 0;
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if($row['num'] == 0) {
		$sSQL = "INSERT INTO " .
				"pu_attribute " .
				"(process_unit, " .
				"alias, " .
				"entity, " .
				"attribute, " .
				"description, " .
				"read_only, " .
				"note)" .
				"VALUES('".sxRAS("process_unit")."', " .
				"'".strtoupper(addslashes(sxGPC(trim($_REQUEST["alias"]))))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["entity"])))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["attribute"])))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["description"])))."', " .
				"'$read_only', " .
				"'".addslashes(sxGPC(trim($_REQUEST["note"])))."'" .
				")";	
		
		$objSDAO->query($sSQL,"ins");
		$process_unit = addslashes(sxGPC($_REQUEST["process_unit"]));
		$alias = addslashes(sxGPC($_REQUEST["alias"]));
		logRecord("300", _t("Inserito nuovo attribute nel processo")." '".$process_unit."'");
		if($redirOnEnd){
			$url = $_SESSION[USEROBJ]->getHistory(0, true);
			serverRedir($url);
		}		
		
	}
	$sSQL = "UPDATE pu_attribute SET " .
			"description='".addslashes(sxGPC(trim($_REQUEST["description"])))."', " .
			"read_only='$read_only', " .
			"note='".addslashes(sxGPC(trim($_REQUEST["note"])))."' " .
			"WHERE process_unit='$process_unit' " .
			"AND entity='$entity'" .
			"AND attribute='$attribute'" .
			"AND alias='$alias'";	
	$objSDAO->query($sSQL,"upd");
	logRecord("500", _t("Aggiornato l'attribute")." '$attribute' "._t("dal processo")." '$process_unit'");
	if($redirOnEnd){
		$url = $_SESSION[USEROBJ]->getHistory(0, true);
		serverRedir($url);
	}
}

